package test.my.com.myapplicationtest.recycleview_expand;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import test.my.com.myapplicationtest.R;

/**
 * Author : Cooper
 * Time : 2017/6/20  14:54
 * Description : ExpandRecycleView 下拉刷新HeaderView
 */

public class RefreshHeaderView extends RelativeLayout {

    public static final int STATUS_REFRESHING = 0x008; // 正在加载
    public static final int STATUS_DONE = 0x005; // 加载完毕
    public static final int STATUS_RELEASE_TO_REFRESH = 0x007; // 准备释放加载
    public static final int STATUS_NORMAL = 0x006; // 正常状态


    private LinearLayout mHeaderContainer;
    private Context mContext;
    private ImageView ivRefresh;
    private TextView tvRefreshHint;
    private AnimationDrawable mRefreshAnimation;
    public int mMeasuredHeight;
    public int mStatus;

    public RefreshHeaderView(Context context) {
        super(context);
        init(context);
    }

    public RefreshHeaderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public RefreshHeaderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        mHeaderContainer = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.erv_refresh_header_layout, this, false);
        ivRefresh = (ImageView) mHeaderContainer.findViewById(R.id.iv_refresh);
        tvRefreshHint = (TextView) mHeaderContainer.findViewById(R.id.tv_refresh_hint);
        mRefreshAnimation = (AnimationDrawable) ivRefresh.getDrawable();
        RelativeLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(CENTER_IN_PARENT);
        addView(mHeaderContainer,layoutParams);
        measure(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        mMeasuredHeight = getMeasuredHeight();
//        setVisibleHeight(0); // 第一次不显示 下拉view
    }

    public void headerMove(float distance) {
        if (getVisibleHeight() > 0 || distance > 0) {
            setVisibleHeight((int)distance + getVisibleHeight());
            if (getVisibleHeight() > mMeasuredHeight) {
                setRefreshStatus(STATUS_RELEASE_TO_REFRESH);
            } else {
                setRefreshStatus(STATUS_NORMAL);
            }
        }
    }

    public int getVisibleHeight() {
        LayoutParams layoutParams = (LayoutParams) mHeaderContainer.getLayoutParams();
        return layoutParams.height;
    }

    private void setVisibleHeight(int height) {
        if (height <= 0) {
            height = 0;
        }
        LayoutParams layoutParams = (LayoutParams) mHeaderContainer.getLayoutParams();
        layoutParams.height = height;
        mHeaderContainer.setLayoutParams(layoutParams);
    }

    public boolean releaseAction() {
        boolean isOnRefresh = false;
        int height = getVisibleHeight();
        if (height == 0) // not visible.
            isOnRefresh = false;

        if(getVisibleHeight() > mMeasuredHeight &&  mStatus == STATUS_RELEASE_TO_REFRESH){
            setRefreshStatus(STATUS_REFRESHING);
            isOnRefresh = true;
        }
        // refreshing and header isn't shown fully. do nothing.
        if (mStatus == STATUS_REFRESHING && height <=  mMeasuredHeight) {
            //return;
        }
        if (mStatus != STATUS_REFRESHING) {
            smoothScrollTo(0);
        }

        if (mStatus == STATUS_REFRESHING) {
            int destHeight = mMeasuredHeight;
            smoothScrollTo(destHeight);
        }

        return isOnRefresh;
    }

    private void smoothScrollTo(int destHeight) {
        ValueAnimator animator = ValueAnimator.ofInt(getVisibleHeight(), destHeight);
        animator.setDuration(300).start();
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation)
            {
                setVisibleHeight((int) animation.getAnimatedValue());
            }
        });
        animator.start();
    }

    public void setRefreshStatus(int status) {
        this.mStatus = status;
        switch (status) {
            case STATUS_NORMAL:
                tvRefreshHint.setText("下拉刷新");
                break;
            case STATUS_RELEASE_TO_REFRESH:
                tvRefreshHint.setText("松手刷新");
                break;
            case STATUS_REFRESHING:
                tvRefreshHint.setText("正在刷新中...");
                mRefreshAnimation.start();
                break;
            case STATUS_DONE:
                tvRefreshHint.setText("刷新完成");
                mRefreshAnimation.stop();
                smoothScrollTo(0);
                break;
        }
    }

}
